//
//  GWChatInputView.m
//  CloudDoctorApp
//
//  Created by hushaohui on 16/5/24.
//  Copyright © 2016年 GoWin. All rights reserved.
//

#import "GWChatInputView.h"
#import "IQKeyboardManager.h"
#import "XMNChat.h"

@interface GWChatInputView ()<XMChatBarDelegate>

@property (nonatomic, strong) XMChatBar* chatBarView;

@end
@implementation GWChatInputView

- (instancetype)initWithView:(UIView *)view {
    self = [super init];
    if (self) {
        IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
        manager.enable = NO;
        self.frame = CGRectMake(0, SCREEN_HEIGHT - kMinHeight, SCREEN_HEIGHT, kMinHeight);
        [self addSubview:self.chatBarView];
    }
    return self;
}
- (void)dealloc{
    //键盘单列控件
    IQKeyboardManager *maneger = [IQKeyboardManager sharedManager];
    maneger.enable = YES;
    maneger.shouldResignOnTouchOutside = YES;
    maneger.shouldToolbarUsesTextFieldTintColor = YES;
    maneger.enableAutoToolbar = NO;
}


- (void)removeChatBar {
    [self.chatBarView endInputing];
}

- (XMChatBar *)chatBarView {
    if (!_chatBarView) {
        _chatBarView = [[XMChatBar alloc]initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH, kMinHeight)];
        _chatBarView.delegate = self;
        [_chatBarView setSuperViewHeight:SCREEN_HEIGHT];
    }
    return _chatBarView;
}

- (void)chatBar:(XMChatBar *)chatBar sendMessage:(NSString *)message {
    if ([self.delegate respondsToSelector:@selector(sendMessage:)]) {
        [self.delegate sendMessage:message];
    }
}
- (void)chatBar:(XMChatBar *)chatBar sendVoice:(NSString *)voiceFileName seconds:(NSTimeInterval)seconds {
    if ([self.delegate respondsToSelector:@selector(sendMessage:)]) {
        [self.delegate sendVoice:voiceFileName withSeconds:seconds];
    }
}
- (void)chatBar:(XMChatBar *)chatBar sendPictures:(NSArray *)pictures {
    if ([self.delegate respondsToSelector:@selector(sendMessage:)]) {
        [self.delegate sendImage:pictures];
    }
}
- (void)chatBarFrameDidChange:(XMChatBar *)chatBar frame:(CGRect)frame {
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, frame.origin.y, SCREEN_HEIGHT, frame.size.height + kFunctionViewHeight);
    }];
    if ([self.delegate respondsToSelector:@selector(InputViewDidChange:)]) {
        [self.delegate InputViewDidChange:frame];
        
    }
}
@end
